
import { PROTOCAL_SEGMENT_BEGIN, PROTOCAL_SEGMENT_END, PROTOCAL_SEGMENT_SEPARATOR } from './base'

const NEW_LINE_CHAR = '\n'
export const PROTOCAL_SEGMENT_TEXT = '0'

export function toHtml(params, id, readonly) {
  return params.text
}

export function toTemplate(params) {
  return PROTOCAL_SEGMENT_BEGIN + PROTOCAL_SEGMENT_TEXT + PROTOCAL_SEGMENT_SEPARATOR + JSON.stringify(params) + PROTOCAL_SEGMENT_END
}

export function newLineToHtml() {
  return toHtml({ text: '<br> ' })
}

export function newLineToTemplate() {
  return toTemplate({ text: NEW_LINE_CHAR })
}

export function parseTextToHtml(segment, segmentValue, id, readonly) {
  if (!segment) {
    return null
  }
  if (segment.startsWith(PROTOCAL_SEGMENT_TEXT)) {
    // console.log(segment, 'parseTextToHtml')
    const segmentString = segment.substring(PROTOCAL_SEGMENT_TEXT.length + PROTOCAL_SEGMENT_SEPARATOR.length)

    const data = JSON.parse(segmentString)
    if (data.text === NEW_LINE_CHAR) {
      return newLineToHtml()
    } else {
      return toHtml(data, id, readonly)
    }
  }
  return null
}

export function toData(params) {
}
